Table of Contents:¶
1. Importing Libraries¶
1.1 Sin and Cos Graph
2. Setting Styles to the Graph¶
2.1 Style
2.2 Size
3. Saving Graphs¶
3.2 Displaying Saved Graphs
4. Different Types of Plots¶
4.1 Line Plots¶
4.1.2 Multiple Line Plots
4.1.3 Boston Dataset
4.1.4 Line Style
4.1.5 Plot Axes
4.1.6 Set
4.2 Scatter Plots¶
4.2.2 Multiple Scatter Plots
4.2.3 Iris Dataset
4.3 Histograms and Binnings¶
4.3.2 Multiple Histograms
4.4 2D Histograms¶
4.5 Pie Charts¶
4.5.2 Basic Operations
4.6 Multiple Subplots¶
4.7 3D Plots¶
1. Importing Matplotlib and some other necessary libraries¶
1 . %matplotlib inline --> used for static plots
2 . %matplotlib notebook --> used for interactive plots
In [219]:
import numpy as np
import matplotlib as mp
import matplotlib.pyplot as plt
%matplotlib inline
In [220]:
np.linspace(0 , np.pi , 20)
Out[220]:
array([0. , 0.16534698, 0.33069396, 0.49604095, 0.66138793, 0.82673491, 0.99208189, 1.15742887, 1.32277585, 1.48812284, 1.65346982, 1.8188168 , 1.98416378, 2.14951076, 2.31485774, 2.48020473, 2.64555171, 2.81089869, 2.97624567, 3.14159265])
- legend()--> this is used to add labels to the the graph given in plot function . The loc argument takes string or integer as the input which tells the location of the legend plot.
- title() function sets the title of the plot.
In [221]:
x = np.linspace(0 , 3*np.pi , 200)
# defining the function
sin_y = np.sin(x)
cos_y = np.cos(x)
# plotting the function
plt.plot(x , sin_y , '*' , label ='Sin')
plt.plot(x , cos_y , '+', label ='Cosine')
plt.xlabel('X')
plt.ylabel('Sin and Cosine')
plt.title('Graph')
plt.legend(loc='upper center')
plt.show()
In [ ]:
2. Setting styles to the graph¶
- plt.style.use('style name') can be used to style the graph
- plt.figure(figsize = (length , height)) --> This is used to resize the plot
In [222]:
plt.style.available
Out[222]:
['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-v0_8', 'seaborn-v0_8-bright', 'seaborn-v0_8-colorblind', 'seaborn-v0_8-dark', 'seaborn-v0_8-dark-palette', 'seaborn-v0_8-darkgrid', 'seaborn-v0_8-deep', 'seaborn-v0_8-muted', 'seaborn-v0_8-notebook', 'seaborn-v0_8-paper', 'seaborn-v0_8-pastel', 'seaborn-v0_8-poster', 'seaborn-v0_8-talk', 'seaborn-v0_8-ticks', 'seaborn-v0_8-white', 'seaborn-v0_8-whitegrid', 'tableau-colorblind10']
In [223]:
x = np.linspace(0 , 3*np.pi , 200)
# defining the function
sin_y = np.sin(x)
cos_y = np.cos(x)
# plotting the function
plt.figure(figsize=(18,5))
plt.plot(x , sin_y , '*' , label ='Sin' , color='green' , linewidth=3)
plt.plot(x , cos_y , '+', label ='Cosine' , color='red' , linewidth=5)
plt.xlabel('X')
plt.ylabel('Sin and Cosine')
plt.title('Graph')
plt.legend(loc='upper right')
# for styling the graph
plt.style.use('grayscale')
# for saving the graph
plt.savefig('sine_cosine.png')
plt.show()
In [224]:
from IPython.display import Image
Image('sine_cosine.png')
Out[224]:
From real world datasets¶
In [225]:
from sklearn.datasets import load_iris
iris = load_iris()
feature = iris['feature_names']
plt.figure(figsize=(18,5))
plt.style.use('ggplot')
plt.plot(feature , color='blue' , linewidth=3)
plt.legend()
plt.show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
In [226]:
from sklearn.datasets import load_diabetes
iris = load_diabetes()
print(iris.keys())
target = iris['target']
plt.figure(figsize=(18,5))
plt.style.use('grayscale')
plt.plot(target[10:100] , color='green' , linewidth=2)
plt.legend()
plt.show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
dict_keys(['data', 'target', 'frame', 'DESCR', 'feature_names', 'data_filename', 'target_filename', 'data_module'])
We can also use linestyle commands . There four major types of linestyle commands:¶
- solid
- dashed
- dashdot
- dotted
In [227]:
x = np.arange(0,20,1)
plt.style.use('grayscale')
plt.figure(figsize=(18,5))
plt.plot(x, x+0 , linestyle='solid' ,color = 'red', label = 'solid' , linewidth=1)
plt.plot(x, x+1 , linestyle='dashed' , color = 'blue', label = 'dashed' , linewidth=2)
plt.plot(x, x+2 , linestyle='dashdot' , color = 'green', label = 'dashdot' , linewidth=3)
plt.plot(x, x+3 , linestyle='dotted' , color = 'black', label = 'dotted' , linewidth=4)
plt.legend()
plt.show()
Adjusting the plot axes¶
- This can be done using xlim and ylim
In [235]:
x = np.linspace(0 , 3*np.pi , 200)
sin_y = np.sin(x)
plt.xlim([-10,20])
plt.ylim([-2,2])
# this can be also be used to set the limit of the graph
# plt.axis('equal')
plt.title('Graph')
plt.plot(x , sin_y , label ='Sin' , color='green' , linewidth=3)
plt.legend()
plt.xlabel('X')
plt.ylabel('SinX')
plt.show()
.set()¶
In [229]:
x = np.linspace(0 , 3*np.pi , 200)
fig = plt.axes()
fig.plot(x,np.sin(x))
fig.set(xlim=(0,10) , ylim=(-2,2) , xlabel='X' , ylabel='Sin(x)' , title='Graph')
fig.plot()
Out[229]:
[]
Scatter Plots:¶
A scatter plot is a type of data visualization in which individual data points are represented as points in a two-dimensional plane. The x-coordinate of each data point represents one feature of the data, and the y-coordinate represents another feature. Scatter plots are commonly used to visualize the relationship between two variables and to identify patterns or outliers in the data.
In Matplotlib, scatter plots can be created using the scatter function.¶
In [230]:
n = 1000
x1 = np.random.randn(n)
y1 = np.random.randn(n)
x2 = np.random.randn(n)+2
y2 = np.random.randn(n)+2
plt.scatter(x1,y1 , color='red' , alpha=0.5 , s=30)
plt.scatter(x2,y2 , color='blue' , alpha=0.7 , s=20)
plt.title('Scatter Plot')
plt.show()
From real world data sets¶
- Alpha: Transparency of the markers in the scatter plot.
- Colorbar: A bar showing the colors used in the plot and the corresponding values.
- Colormap: A color mapping used to convert scalar data to colors.
- Edgecolors: The color of the edges of the markers in a scatter plot.
- Sizes: The size of the markers in a scatter plot.
In [250]:
from sklearn.datasets import load_iris
iris = load_iris()
x = iris.data
y = iris.target
plt.scatter(x[:,2],x[:,3], c = 'blue', s =50, alpha = .9 , marker='+')
plt.xlabel(iris['feature_names'][2])
plt.ylabel(iris['feature_names'][3])
plt.colorbar()
plt.show()
For iris dataset¶
In [262]:
iris = load_iris()
features = iris.data
print(iris.feature_names)
plt.scatter(features[:,0], features[:,1], alpha = .8, s = features[:,3]*100, c = iris.target, cmap ='viridis')
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1])
plt.colorbar()
plt.show()
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
In [269]:
rng = np.random.RandomState(0)
x = rng.randn(100)
y = rng.randn(100)
colors = rng.rand(100)
sizes = rng.rand(100) * 1000
plt.scatter(x,y, c = colors, s = sizes , alpha = 0.8, cmap = 'viridis', edgecolors = 'black')
plt.title('Random Scatter Plot')
plt.xlabel('x values')
plt.ylabel('y values')
plt.colorbar()
plt.show()
In [274]:
iris.target_names
Out[274]:
array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
Histogram and Binning¶
- A histogram is a plot that displays the distribution of a set of continuous or discrete data.
- The data is divided into a set of intervals or bins, and the height of each bar in the plot represents the number of data points that fall within that bin.
- Binnings refers to the process of dividing the data into these bins. The choice of bin size can greatly affect the appearance of the histogram, so it's important to choose a bin size that accurately represents the underlying distribution of the data.
Individual Bins can be removed using histtype='stepfilled'¶
In [310]:
data = np.random.randn(10000000)
plt.hist(data , bins=1000, alpha=0.8 , color='blue' , edgecolor='black' )
plt.title('Random Histogram')
plt.ylabel('Frequency')
plt.xlabel('Values')
plt.show()
- Multiple Histogams
In [316]:
x1 = np.random.randn(1000)
x2 = np.random.randn(1000) + 2
x3 = np.random.randn(1000) + 4
plt.hist(x1, bins = 20, alpha = .8 , color='red' , edgecolor='black')
plt.hist(x2, bins = 20, alpha = .8 , color='black' , edgecolor='black')
plt.hist(x3, bins = 20, alpha = .8 , color='green' , edgecolor='black')
plt.title('Random Histogram')
plt.ylabel('Frequency')
plt.xlabel('Values')
plt.show()
2D Histograms¶
- plt.hist2d is a function in matplotlib that generates a 2D histogram.
- A 2D histogram is a representation of the distribution of data over two variables.
- In contrast to a regular histogram which shows the distribution of a single variable, a 2D histogram shows the relationship between two variables.
- The function creates a heatmap that shows the density of data points in a 2D space.
In [339]:
x = np.random.randn(1000)
y = np.random.randn(1000)+2
plt.hist2d(x,y , bins=30 , color='blue' , edgecolor='black' , alpha=0.8 , cmap='viridis' )
plt.title('2D Histogram')
plt.ylabel('x')
plt.xlabel('y')
plt.colorbar()
plt.show()
# plt.hist2d(x,y, bins = 20 , cmap = 'Blues')
In [335]:
plt.hist2d(x,y, bins = 20 , cmap = 'Blues')
plt.title('2D Histogram')
plt.ylabel('x')
plt.xlabel('y')
plt.colorbar()
plt.show()
Pie Charts¶
- A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportion.
In [338]:
cars = ['AUDI','BMW','FORD','TESLA','JAGUAR','MERCEDES']
data = [ 450 , 385 , 35 , 10 , 200 , 500 ]
fig = plt.figure(figsize = (7,7))
plt.pie(data, labels = cars , colors=['red' , 'blue' , 'green' , 'yellow' , 'black' , 'pink'])
plt.xlabel('Cars')
plt.ylabel('Frequency')
plt.title('Pie Chart')
plt.show()
- Basic operations
In [358]:
cars = ['AUDI','BMW','FORD','TESLA','JAGUAR','MERCEDES']
data = [ 450 , 385 , 35 , 100 , 200 , 500 ]
explode = (0, 0, 0.1, 0.2, 0.1, 0) # This is to pull out the specific pie inside the chart
fig = plt.figure(figsize = (7,7))
plt.pie(data, labels = cars , colors=['red' , 'cyan' , 'green' , 'blue' , 'black' , 'pink'], explode=explode , autopct = '%1.2f%%' , shadow = True)
plt.xlabel('Cars')
plt.ylabel('Frequency')
plt.title('Pie Chart')
plt.show()
In [360]:
cars = ['AUDI','BMW','FORD','TESLA','JAGUAR','MERCEDES']
data = [ 450 , 385 , 35 , 100 , 200 , 500 ]
explode = (0, 0, 0.1, 0.2, 0.1, 0) # This is to pull out the specific pie inside the chart
fig = plt.figure(figsize = (7,7))
plt.pie(data, labels = cars , colors=['red' , 'cyan' , 'green' , 'blue' , 'black' , 'pink'] , wedgeprops = {'edgecolor' : 'black', 'linewidth' : 2, 'antialiased' : True})
plt.xlabel('Cars')
plt.ylabel('Frequency')
plt.title('Pie Chart')
plt.show()
Multiple Subplots¶
In [374]:
x = np.linspace(0,10,50)
print(x)
fig ,ax = plt.subplots(2,2)
y = np.sin(x)
ax[0,0].plot(x,y)
ax[0,0].set_title('Sin Wave')
y = np.cos(x)
ax[0,1].plot(x,y)
ax[0,1].set_title('Cos Wave')
y = np.tan(x)
ax[1,0].plot(x,y)
ax[1,0].set_title('Tan Wave')
y = np.random.randn(1000)
ax[1,1].hist(y,bins = 40)
ax[1,1].set_title('Random Distribution')
plt.tight_layout()
plt.show()
[ 0. 0.20408163 0.40816327 0.6122449 0.81632653 1.02040816 1.2244898 1.42857143 1.63265306 1.83673469 2.04081633 2.24489796 2.44897959 2.65306122 2.85714286 3.06122449 3.26530612 3.46938776 3.67346939 3.87755102 4.08163265 4.28571429 4.48979592 4.69387755 4.89795918 5.10204082 5.30612245 5.51020408 5.71428571 5.91836735 6.12244898 6.32653061 6.53061224 6.73469388 6.93877551 7.14285714 7.34693878 7.55102041 7.75510204 7.95918367 8.16326531 8.36734694 8.57142857 8.7755102 8.97959184 9.18367347 9.3877551 9.59183673 9.79591837 10. ]
In [370]:
plt.subplots_adjust(hspace = .4, wspace = .4)
for i in range(1,7):
plt.subplot(2,3,i)
plt.text( 0.5,0.5,str((2,3,i)), fontsize = 18, ha = 'center')
In [376]:
ax = plt.axes(projection = '3d')
z = np.linspace(0,15,10)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x,y,z, 'blue')
Out[376]:
[<mpl_toolkits.mplot3d.art3d.Line3D at 0x1a69e910f50>]
In [395]:
ax = plt.axes(projection = '3d')
z = np.linspace(0,15,1000)
x = np.cos(z)
y = np.tan(z)
ax.plot3D(x,y,z, 'blue')
Out[395]:
[<mpl_toolkits.mplot3d.art3d.Line3D at 0x1a691ea7cd0>]
In [392]:
# from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
ax.scatter(x,y,z, c = 'red', marker = '*')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.tight_layout()
plt.show()
<Figure size 640x480 with 0 Axes>
Bars¶
In [397]:
cars = ['AUDI','BMW','FORD','TESLA','JAGUAR','MERCEDES']
data = [ 450 , 385 , 35 , 10 , 200 , 500 ]
# fig = plt.figure(figsize = (7,7))
plt.bar(cars, data, color=['red' , 'blue' , 'green' , 'yellow' , 'black' , 'pink'])
plt.xlabel('Cars')
plt.ylabel('Frequency')
plt.title('Bar Chart')
plt.show()
In [ ]: